/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
import Chess.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;

/**
 *
 * @author LostOdyssey
 */
public class OthelloGui {
        public static void main(String agrs[]) throws IOException  {
            MyGui myGui = new MyGui("Othello");
            myGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
            myGui.setLocationRelativeTo(null);
            myGui.setResizable(false);
            myGui.setVisible(true);                   
        }
}
class MyGui extends JFrame implements ActionListener {        
    private static JPanel myPanel = new JPanel(new GridLayout(10,10,1,1));
    boolean wholeTurn = true;              
    private static JButton[][] myButton = new JButton[10][10];
    private int countDown = 96;
    final Image img1 = ImageIO.read(getClass().getResource("Black-Token.gif")); 
    final Image img2 = ImageIO.read(getClass().getResource("Black-Token-Freeze.gif"));        
    Icon icopreview1 = new ImageIcon(img1);
    Icon icopreview2 = new ImageIcon(img2);
    ActionChess actionChess = new ActionChess();
            
    public MyGui(String title) throws IOException {
        super(title);  
        setLayout(null);
        setSize(750,560);          
        /*JPanel wholePanel = new JPanel();
        wholePanel.setSize(750,560);
        wholePanel.setBackground(Color.WHITE);
        this.add(wholePanel);        */
        myPanel.setSize(480,480);
        myPanel.setLocation(35,25); 
        myPanel.setBorder(new LineBorder(Color.DARK_GRAY,4, true));
        myPanel.setBackground(Color.black);  
        add(myPanel);
        //wholePanel.add(myPanel);
        for(int i = 0; i < 10; i++) {
            for(int j = 0; j < 10; j++)
            {                
                myButton[i][j] = new JButton(); 
                if(actionChess.getValueOfChess(i, j)== -1) {                                        
                    myButton[i][j].setBackground(Color.LIGHT_GRAY);
                }
                if(actionChess.getValueOfChess(i, j)== 0) {  
                    myButton[i][j].setBackground(Color.LIGHT_GRAY);                                        
                    myButton[i][j].setIcon(icopreview1);                     
                }
                if(actionChess.getValueOfChess(i, j)== 1) {   
                    myButton[i][j].setBackground(Color.LIGHT_GRAY);                                        
                    myButton[i][j].setIcon(icopreview2);                       
                }
                myButton[i][j].addActionListener(this);               
                myPanel.add(myButton[i][j]);
            }
        }
    }           
    
    private void rePaintBoard() {
        for(int x = 0; x < 10; x++) {
            for(int y = 0; y < 10; y++)
            {                                
                if(actionChess.getValueOfChess(x, y)== -1) {                                        
                    myButton[x][y].setBackground(Color.LIGHT_GRAY);
                }
                if(actionChess.getValueOfChess(x, y)== 0) {  
                    myButton[x][y].setBackground(Color.LIGHT_GRAY);
                    myButton[x][y].setIcon(icopreview1);                     
                }
                if(actionChess.getValueOfChess(x, y)== 1) {   
                    myButton[x][y].setBackground(Color.LIGHT_GRAY);                                        
                    myButton[x][y].setIcon(icopreview2);                       
                }                                
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent ae)  {
            // TODO Auto-generated method stub           
          for(int i=0; i<10; i++) {
               for(int j=0; j<10; j++){
                    if(ae.getSource()== myButton[i][j]&& wholeTurn== true && actionChess.getValueOfChess(i, j)== -1){
                        if(actionChess.checkChess(i, j, 0) == true) { 
                            actionChess.setChessBoard(i, j, 0);
                            actionChess.changeChessBoard(i, j, 0);
                            this.rePaintBoard();                              
                            wholeTurn = false;  
                            countDown--;
                        }                             
                            actionChess.inbancon(i, j);
                            System.out.println(wholeTurn);                            
                    }

                    if(ae.getSource()== myButton[i][j] && wholeTurn== false && actionChess.getValueOfChess(i, j)== -1){
                        if(actionChess.checkChess(i, j, 1) == true) { 
                            actionChess.setChessBoard(i, j, 1);
                            actionChess.changeChessBoard(i, j, 1);
                            this.rePaintBoard();                   
                            wholeTurn = true;     
                            countDown--;                            
                        }    
                        actionChess.inbancon(i, j);
                        System.out.println(wholeTurn);                            
                    }
                        
                        
                        
                    }
                }
           }
    
    

}
    
    
    
  


